package com.qp.bookkeeping.business.book.controller;

import com.qp.bookkeeping.business.book.model.Do.ShareGroupDo;
import com.qp.bookkeeping.business.book.model.req.*;
import com.qp.bookkeeping.business.book.model.rsp.UserInfoByPhoneRsp;
import com.qp.bookkeeping.business.book.model.rsp.UserInfoRsp;
import com.qp.bookkeeping.business.book.service.UserService;
import com.qp.bookkeeping.business.common.Exception.BusinessException;
import com.qp.bookkeeping.common.model.rsp.BaseRsp;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xutianqi
 */
@RestController
@RequestMapping("user")
public class UserConntroller {
    @Resource
    UserService userService;

    @PostMapping("saveUser")
    public BaseRsp saveUser(@RequestBody SaveUserReq saveUserReq) throws Exception {
        try {
            userService.saveUser(saveUserReq.getUser());
        } catch (Exception e) {
            throw new Exception(e);
        }
        return BaseRsp.success();
    }

    @PostMapping("changePhone")
    public BaseRsp changePhone(@RequestBody UpdateUserReq updateUserReq) {
        try {
            userService.changePhone(updateUserReq);
        } catch (BusinessException e) {
            return BaseRsp.fail(e.getCode(), e.getMessage());
        }
        return BaseRsp.success();
    }

    @PostMapping("updateUser")
    public BaseRsp updateUser(@RequestBody UpdateUserReq updateUserReq) {
        try {
            userService.updateUser(updateUserReq);
        } catch (BusinessException e) {
            return BaseRsp.fail(e.getCode(), e.getMessage());
        }
        return BaseRsp.success();
    }

    @PostMapping("queryUserInfo")
    public BaseRsp<UserInfoRsp> queryUserInfo(@RequestBody QueryUserInfoReq queryUserInfoReq) {
        UserInfoRsp userInfoRsp;
        try {
            userInfoRsp = userService.queryUserInfo(queryUserInfoReq);
        } catch (BusinessException e) {
            return BaseRsp.fail(e.getCode(), e.getMessage());
        }
        return BaseRsp.success(userInfoRsp);
    }

    @PostMapping("queryUserInfoByPhone")
    public BaseRsp<UserInfoByPhoneRsp> queryUserInfoByPhone(@RequestBody QueryUserInfoReq queryUserInfoReq) {
        UserInfoByPhoneRsp userInfoByPhoneRsp;
        try {
            userInfoByPhoneRsp = userService.queryUserInfoByPhone(queryUserInfoReq);
        } catch (BusinessException e) {
            return BaseRsp.fail(e.getCode(), e.getMessage());
        }
        return BaseRsp.success(userInfoByPhoneRsp);
    }

    /*----------------------------------group----------------------------------*/
    @PostMapping("createGroup")
    public BaseRsp createGroup(@RequestBody CreateGroupReq createGroupReq) throws Exception {
        try {
            userService.createGroup(createGroupReq);
        } catch (Exception e) {
            throw new Exception(e);
        }
        return BaseRsp.success();
    }

    @PostMapping("queryGroup")
    public BaseRsp<List<ShareGroupDo>> queryGroup(@RequestBody QueryGroupReq queryGroupReq) throws BusinessException {
        List<ShareGroupDo> shareGroupDos;
        try {
            shareGroupDos = userService.queryGroup(queryGroupReq);
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
        return BaseRsp.success(shareGroupDos);
    }

    @PostMapping("updateGroupName")
    public BaseRsp updateGroupName(@RequestBody UpdateGroupReq updateGroupReq) throws BusinessException {
        try {
            userService.updateGroupName(updateGroupReq);
        } catch (BusinessException e) {
            return BaseRsp.fail(e.getCode(), e.getMessage());
        }
        return BaseRsp.success();
    }

    @PostMapping("joinGroup")
    public BaseRsp joinGroup(@RequestBody JoinGroupReq joinGroupReq) {
        try {
            userService.joinGroup(joinGroupReq);
        } catch (BusinessException e) {
            return BaseRsp.fail(e.getCode(), e.getMessage());
        }
        return BaseRsp.success();
    }

    @PostMapping("deleteMember")
    public BaseRsp deleteMember(@RequestBody ShareGroupDo shareGroupDo) {
        try {
            userService.deleteMember(shareGroupDo);
        } catch (BusinessException e) {
            return BaseRsp.fail(e.getCode(), e.getMessage());
        }
        return BaseRsp.success();
    }
}
